/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package homebudget;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.LinkedList;

/**
 *
 * @author KriT
 */
public class Report {
    NumberFormat nf = NumberFormat.getInstance();
    LinkedList<String> expensesCategories = new LinkedList<String>();
    LinkedList<String> contributionsCategories = new LinkedList<String>();
    LinkedList<Double> expensesAmounts = new LinkedList<Double>();
    LinkedList<Double> contributionsAmounts = new LinkedList<Double>();
    double expensesTotal = 0;
    double contributionsTotal = 0;
    String date;

    public Report(String s, DataBase db) {
        date = s;

        SimpleDateFormat sdf = new SimpleDateFormat("MMM,''yy");

        for (Entry e : db.expenses) {
            String d = sdf.format(e.date);

            if (date.equals(d)) {
                String c = e.category;
                if (!expensesCategories.contains(c)) {
                    expensesCategories.add(c);
                    expensesAmounts.add(e.wartosc);    //e.price * e.quantity);
                } else {
                    int index = expensesCategories.indexOf(c);
                    double amount = expensesAmounts.get(index);
                    amount += e.wartosc;//e.price * e.quantity;
                    expensesAmounts.set(index, amount);

                }
            }

        }

        for (Entry e : db.contributions) {
            String d = sdf.format(e.date);

            if (date.equals(d)) {
                String c = e.category;
                if (!contributionsCategories.contains(c)) {
                    contributionsCategories.add(c);
                    contributionsAmounts.add(e.wartosc);
                } else {
                    int index = contributionsCategories.indexOf(c);
                    double amount = contributionsAmounts.get(index);
                    amount += e.wartosc ;                    
                    contributionsAmounts.set(index, amount);

                }
            }

        }

        for (double f : expensesAmounts) {
            expensesTotal += f;
        }

        for (double f : contributionsAmounts) {
            contributionsTotal += f;
        }
    }

    public String getString() {
        nf.setMinimumFractionDigits(2);
        String result = new String();

        result += "\tZestawienie z " + date + ":\n";
        result += "\nWydatki na: \n";
        for (int i = 0; i < expensesAmounts.size(); i++) {
            result += expensesCategories.get(i) + ": \t\t" + nf.format(expensesAmounts.get(i)) + "\n";
        }

        result += "\nZarobki z : \n";
        for (int i = 0; i < contributionsAmounts.size(); i++) {
            result += contributionsCategories.get(i) + ": \t\t" + nf.format(contributionsAmounts.get(i)) + "\n";
        }
        
        result += "\nSaldo wynosi: \t\t" + nf.format(contributionsTotal - expensesTotal);

        return result;
    }
}
